﻿using Controller.Enum;
using Controller.Model;
using DPS.ControllerClient;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace AioiTestDemo
{
    public partial class Form1 : Form
    {
        string ip;
        int port;

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 亮灯
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ActionPacketModel action = new ActionPacketModel
            {
                EquipmentBrand = EquipmentBrandEnum.AIOI,
                LabelType = EquipmentLabelTypeEnum.AIOI_LABEL_DEFAULT,
                ControllerKey = $"{ip}:{port}",
                AddressList = new List<string> { "1001" },
                ControlModel = new DeviceActionModel
                {
                    IsUsedConfirm = true,
                    LightModel = CommCmdLightModeEnum.GREEN,
                    Seq = "111",
                    ConfirmLightModel = CommCmdLightModeEnum.PURPLE,
                    ConfirmOpenBuzzer = DeviceBuzzerStatuEnum.SPARKLE,
                    OpenBuzzer = DeviceBuzzerStatuEnum.SPARKLE,
                    ConfirmSeq = "222",
                },
            };
            GetController().Control(action);
        }

        /// <summary>
        /// 灭灯
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            List<string> addressList = new List<string>();
            addressList.Add("1001");
            GetController().Control(new CancelPacketModel
            {
                EquipmentBrand = EquipmentBrandEnum.AIOI,
                LabelType = EquipmentLabelTypeEnum.AIOI_LABEL_DEFAULT,
                AddressList = addressList,
                ControllerKey = $"{ip}:{port}"
            });
        }

        /// <summary>
        /// 文字显示屏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            ActionPacketModel action = new ActionPacketModel
            {
                EquipmentBrand = EquipmentBrandEnum.AIOI,
                LabelType = EquipmentLabelTypeEnum.AIOI_LABEL_MWU2414,
                ControllerKey = $"{ip}:{port}",
                AddressList = new List<string> { "1003" },
                ControlModel = new DeviceActionModel {
                    IsUsedConfirm = true,
                    LightModel = CommCmdLightModeEnum.GREEN,
                    Seq = "111",
                    ConfirmLightModel = CommCmdLightModeEnum.PURPLE,
                    ConfirmOpenBuzzer = DeviceBuzzerStatuEnum.SPARKLE,
                    OpenBuzzer = DeviceBuzzerStatuEnum.SPARKLE,
                    ConfirmSeq = "222",
                },
            };
            action.LabelType = EquipmentLabelTypeEnum.AIOI_LABEL_DEFAULT;
            GetController().Control(action);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ip = "192.168.1.25";
            port = 5003;
        }

        /// <summary>
		/// 获取控制器
		/// </summary>
		/// <param name="device"></param>
		/// <returns></returns>
		public BaseController GetController()
        {
            BaseController controller = ControllerFactory.GetInstance().Get("127.0.0.1", 9008, DeviceButtonEvent, DeviceExceptionEvent, 
                DeviceStateEvent, ControllerConnectionStateChangeEvent, ControllerManagerStateEvent);
            return controller;
        }


        /// <summary>
        /// 控制器触发设备按钮事件
        /// </summary>
        /// <param name="iModel"></param>
        protected void DeviceButtonEvent(TriggerLabelPacketModel iModel)
        {
            if (!string.IsNullOrEmpty(iModel.EpcData)) {
                Console.WriteLine(iModel.EpcData);//E20000006A130084100035F6
                //GetController().Control(new CancelPacketModel
                //{
                //    AddressList = new List<string> { iModel.Address },
                //    ControllerKey = iModel.ControllerKey,
                //    EquipmentBrand = EquipmentBrandEnum.AIOI,
                //    LabelType = EquipmentLabelTypeEnum.AIOI_LABEL_DEFAULT
                //});
            }
        }

        /// <summary>
		/// 控制器的设备异常事件
		/// </summary>
		/// <param name="iKey"></param>
		/// <param name="iDeviceAddress"></param>
		/// <param name="iExceptionCode"></param>
		protected void DeviceExceptionEvent(string iKey, string iDeviceAddress, ControllerExceptionCodeEnum iExceptionCode)
        {

        }

        /// <summary>
		/// 控制器设备状态事件
		/// </summary>
		/// <param name="iModel"></param>
		protected void DeviceStateEvent(LabelStatePacketModel iModel)
        {
        }


        /// <summary>
        /// 控制器连接状态修改事件
        /// </summary>
        /// <param name="iModel"></param>
        internal void ControllerConnectionStateChangeEvent(ControllerStatePacketModel iModel)
        {
        }

        /// <summary>
		/// 控制器管理器连接状态修改事件
		/// </summary>
		/// <param name="iState"></param>
		internal void ControllerManagerStateEvent(bool iState)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            ActionPacketModel action = new ActionPacketModel
            {
                EquipmentBrand = EquipmentBrandEnum.AIOI,
                LabelType = EquipmentLabelTypeEnum.AIOI_LABEL_MWU2060,
                ControllerKey = $"{ip}:{port}",
                AddressList = new List<string> { "1003" ,"1004"},
                ControlModel = new DeviceActionModel
                {
                    IsUsedConfirm = true,//是否需要感应
                    LightModel = CommCmdLightModeEnum.GREEN,
                    Seq = "111",
                    ConfirmLightModel = CommCmdLightModeEnum.PURPLE,
                    ConfirmOpenBuzzer = DeviceBuzzerStatuEnum.SPARKLE,
                    OpenBuzzer = DeviceBuzzerStatuEnum.SPARKLE,
                    ConfirmSeq = "222",
                },
            };
            if(!string.IsNullOrEmpty(textBoxRfid.Text))
                action.ControlModel.UhfRfId = textBoxRfid.Text.PadLeft(24, ' ').Substring(0, 24);
            //if (string.IsNullOrEmpty(textBoxRfid.Text))
            //{
            //    action.ControlModel.UhfRfId = "999999999999999999999999";
            //}
            //else {
            //    //  action.ControlModel.UhfRfId = textBoxRfid.Text;
            //}
            GetController().Control(action);
        }
    }
}
